package cn.genmer.test.security.leetcode;

/**
 * @program: Algorithm_to_practice
 * @description: 原地算法 - 翻转数组
 * @author: Genmer
 * @create: 2020-10-14 17:06
 **/
public class Inplace {

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        System.out.println("翻转前的数组：");
        for (int i =0;i<array.length;i++){
            System.out.print(array[i]);

            if (i == array.length-1) System.out.println();
            else System.out.print(",");
        }
        reserveArray(array);
        System.out.println("翻转后的数组：");
        for (int i =0;i<array.length;i++){
            System.out.print(array[i]);
            if (i == array.length-1) System.out.println();
            else System.out.print(",");
        }
    }

    /**
     * 应该是自己写的笨办法
     * @param arr
     */
    public static void reserveArray(int[] arr){
        int len = arr.length;
        int mid = (int) Math.floor(len/2);
        for (int i=0;i<mid;i++){
            int temp = arr[i];
            arr[i] = arr[len-1-i];
            arr[len-1-i]= temp;
        }
    }
}
